package service

import (
	"server/global"
	"server/plugin/mall/model"
	"server/plugin/mall/model/request"
)

var A类别设置 = new(leibieshezhi)

type leibieshezhi struct{}

// CreateA类别设置 创建类别设置记录
// Author [yourname](https://github.com/yourname)
func (s *leibieshezhi) CreateA类别设置(leibieshezhi *model.A类别设置) (err error) {
	err = global.GVA_DB.Create(leibieshezhi).Error
	return err
}

// DeleteA类别设置 删除类别设置记录
// Author [yourname](https://github.com/yourname)
func (s *leibieshezhi) DeleteA类别设置(ID string) (err error) {
	err = global.GVA_DB.Delete(&model.A类别设置{}, "id = ?", ID).Error
	return err
}

// DeleteA类别设置ByIds 批量删除类别设置记录
// Author [yourname](https://github.com/yourname)
func (s *leibieshezhi) DeleteA类别设置ByIds(IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]model.A类别设置{}, "id in ?", IDs).Error
	return err
}

// UpdateA类别设置 更新类别设置记录
// Author [yourname](https://github.com/yourname)
func (s *leibieshezhi) UpdateA类别设置(leibieshezhi model.A类别设置) (err error) {
	err = global.GVA_DB.Model(&model.A类别设置{}).Where("id = ?", leibieshezhi.ID).Updates(&leibieshezhi).Error
	return err
}

// GetA类别设置 根据ID获取类别设置记录
// Author [yourname](https://github.com/yourname)
func (s *leibieshezhi) GetA类别设置(ID string) (leibieshezhi model.A类别设置, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&leibieshezhi).Error
	return
}

// GetA类别设置InfoList 分页获取类别设置记录
// Author [yourname](https://github.com/yourname)
func (s *leibieshezhi) GetA类别设置InfoList(info request.A类别设置Search) (list []model.A类别设置, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&model.A类别设置{})
	var leibieshezhis []model.A类别设置
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}
	err = db.Find(&leibieshezhis).Error
	return leibieshezhis, total, err
}
func (s *leibieshezhi) GetA类别设置DataSource() (res map[string][]map[string]any, err error) {
	res = make(map[string][]map[string]any)

	父级id := make([]map[string]any, 0)

	global.GVA_DB.Table("类别设置").Where("deleted_at IS NULL").Select("类别 as label,id as value").Scan(&父级id)
	res["父级id"] = 父级id
	return
}

func (s *leibieshezhi) GetA类别设置Public() {

}
